<?php
declare(strict_types=1);

namespace app\api\controller\admin;


use app\common\model\delivery\Channel as ChannelModel;
use app\common\model\delivery\Seller as SellerModel;
use app\common\model\delivery\SellerCustomer;
use think\annotation\route\Get;
use think\annotation\route\Group;
use think\annotation\route\Post;
use think\facade\Db;

#[Group("admin/channel")]
class Channel extends Base
{
    protected function _initialize()
    {
        parent::_initialize();
        if($this->seller){
            $this->error('你没有操作权限');
        }
    }

    #[Get('listSeller')]
    public function listSeller()
    {
        $list=SellerModel::with(['user'])->where(['channel_id'=>$this->channel->id])->select();
        $this->success('',$list);
    }

    #[Post('addSeller')]
    public function addSeller()
    {
        $postdata=$this->request->post();
        if(!isset($postdata['user_id'])){
            $this->error('绑定微信为必填项目');
        }
        $hseller=SellerModel::where(function ($query) use ($postdata){
            $query->where('channel_id','=',$this->channel->id);
            $query->where('user_id','=',$postdata['user_id']);
            if($postdata['id']){
                $query->where('id','<>',$postdata['id']);
            }
        })->find();
        if($hseller){
            $this->error('该微信已经绑定了别的销售经理');
        }
        if($postdata['id']){
            $seller=SellerModel::find($postdata['id']);
        }else{
            $seller=new SellerModel();
            $postdata['channel_id']=$this->channel->id;
        }
        $seller->save($postdata);
        $this->success('操作成功');
    }

    #[Get('delSeller')]
    public function delSeller($id)
    {
        $seller=SellerModel::find($id);
        $seller->delete();
        $this->success('删除成功');
    }

    #[Post('setcomm')]
    public function setcomm()
    {
        $postdata=$this->request->post();
        if($postdata['commission_type']=='persent'){
            if($postdata['persent']>100 || $postdata['persent']<0){
                $this->error('提成比例在0到100之间！');
            }
        }
        if($postdata['commission_type']=='money'){
            if($postdata['money']<=0){
                $this->error('提成金额不能小于等于0！');
            }
        }
        $sellerstore=SellerCustomer::where('customer_id',$postdata['customer_id'])->find();
        if(!$sellerstore){
            $sellerstore=new SellerCustomer();
            $sellerstore->customer_id=$postdata['customer_id'];
        }
        $sellerstore->commission_type=$postdata['commission_type'];
        $sellerstore->money=$postdata['money'];
        $sellerstore->persent=$postdata['persent'];
        $sellerstore->save();
        $this->success('设置成功',$sellerstore);
    }

    #[Get('wuliu')]
    public function wuliu()
    {
        $wuliu=ChannelModel::wuliu($this->channel);
        $this->success('',$wuliu);
    }

    #[Get('driverposition')]
    public function driverposition($gird_id='',$driver_id='')
    {
        if($gird_id){
            $driver=ChannelModel::driver([$gird_id]);
            $this->success('',$driver);
        }
        if($driver_id){
            $page=$this->request->get('page/d');
            $limit='';
            if($page>0){
                $offset=$page-1;
                $limit="LIMIT {$offset},1";
            }
            $time=strtotime(date('Y-m-d 00:00:00',time()));
            $prefix=getDbPrefix();
            $sql="
                SELECT createtime,latitude,longitude
                FROM
                (
                SELECT
                createtime,
                latitude,longitude,
                LEFT (latitude, 7) AS glatitude,
                LEFT (longitude, 8) AS glongitude
                FROM {$prefix}driver_position
                WHERE
                createtime > {$time} AND driver_id = {$driver_id}
                )t1 GROUP BY glatitude,glongitude
                order by createtime asc
                {$limit}
            ";
            $list=Db::query($sql);
            if(count($list)==0){
                $this->error('已经是司机最后的位置了');
            }
            foreach ($list as &$value){
                $value['createtime']=date('H:i',$value['createtime']);
            }
            $this->success('',$list);
        }
    }
}
